Skip to content

Treat an argument with no known refinement as unconstrained - #496

Merged
CatarinaGamboa merged 2 commits into
mainfrom
fix/451-sort-mismatch
Nov 6, 2026
Merged

CatarinaGamboa merged 2 commits into
mainfrom
fix/451-sort-mismatch

Conversation

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator

Fixes #451

Cause. When a call argument carries no information (its refinement is just true, as for the result of a method without refinements), createVariableRepresentingArgument still wrapped it as arg == true. For an int argument Z3 rejects that with "Sorts Int and Bool are incompatible". For a boolean argument it silently assumed the value was true, so takeTrue(s.isEmpty()) against @Refinement("_ == true") was accepted. Now such an argument is left unconstrained, like any unknown value.

Study exercise A2_04, fixed with socket.connect(addr, Math.max(timeout, 0)) (no spec for Math.max):

Before:

Error: Sorts Int and Bool are incompatible
101 |             socket.connect(new InetSocketAddress(host, port), Math.max(timeout, 0));

After:

Refinement Error: expected Math.max(timeout, 0) >= 0 but could not prove it
101 |             socket.connect(new InetSocketAddress(host, port), Math.max(timeout, 0));
 --> Counterexample: Math.max(timeout, 0) == -1

The other A2_04 fixes (guard + return, guard + throw, ternary) still verify. With a Math.max spec (_ >= a && _ >= b && (_ == a || _ == b)) the Math.max fix verifies too. The issue's new Socket(addr, server.getLocalPort()) now reports expected server.getLocalPort() >= 0 && server.getLocalPort() <= 65535 but could not prove it.

Tests. Two cases added to ErrorUnknownMethodInOperation: an unknown int result and an unknown boolean result passed as arguments. Both fail on main (the first crashes, the second is accepted).

🤖 Generated with Claude Code

CatarinaGamboa and others added 2 commits November 6, 2026 12:30
An argument whose refinement is just `true` (e.g. the result of a method
without refinements) was turned into `arg == true`. For an int argument
that crashed with "Sorts Int and Bool are incompatible"; for a boolean one
it wrongly assumed the value was true.

Fixes #451

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 1

  • [P2] Preserve known-true boolean argument values: isBooleanTrue() also holds for an expression such as true && true, so takeTrue(true && true) against _ == true lost its value and was rejected.

Fixed in 54a6de7: the equality is now skipped only when the argument's refinement is exactly the literal true (the placeholder for "nothing known"). takeTrue(true && true) and takeTrue(true) verify again; full suite passes.

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 2

No findings: "The change prevents an unknown argument refinement from being treated as the literal value true. The verifier test suite passes, including the new argument tests."

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Clean-code review (clean-code-principles)

Reviewed the diff only, weighed against the standing rules (smallest PR that tests well, match surrounding style, no tidy-only refactors, lean tests). Nothing worth changing; no new commits.

Finding Verdict
MethodsFunctionsChecker.java:462 - [core-dry] met.getExpression() instanceof LiteralBoolean b && b.isBooleanTrue() looks like it could be met.isBooleanTrue() (already on Predicate), dropping the new import Declined. It is not the same check. Predicate.isBooleanTrue() also accepts compound trivially-true expressions such as true && true, which carry information about the argument and must still become _ == (...). The literal-only test is the precise "nothing is known" case.
MethodsFunctionsChecker.java:460 - [doc-why-not-what] new comment OK. It explains why a bare true is skipped, not what the code does.
MethodsFunctionsChecker.java:462 - [name-meaningful] unknown OK. The name matches the comment's meaning.
ErrorUnknownMethodInOperation.java:17 - (issue #451) in a comment Declined. It is a test fixture, where issue refs are allowed (the no-#N rule applies to verifier sources).
Tests - [lean tests] two new fixture methods (int and boolean argument) OK. Each method covers one sort of the crash, and both are in an existing fixture.

@CatarinaGamboa
CatarinaGamboa merged commit 8958c70 into main Nov 6, 2026
1 check passed
CatarinaGamboa added a commit that referenced this pull request Nov 23, 2026
Fixes #448

## Root cause

`RefinementTypeChecker.visitCtNewArray` builds the `length(_) == n`
refinement of a new array only from its size expressions (`new
byte[n]`). An array initializer (`new byte[] {1, 2}`, or `{...}` in a
declaration) has no size expression, so the new array got no refinement
at all and its length was unknown.

When the issue was filed, the argument with no refinement was wrapped as
`arg == true`, which Z3 rejected (`Sorts |byte[]| and Bool are
incompatible`). Since #496 (an argument with no known refinement is
unconstrained), the crash is gone, but the correct call is still
rejected because nothing is known about `length(b)`.

## Fix

When a `CtNewArray` has no size expressions, its refinement is
`length(_) == <number of elements>`. This covers every element type,
nested initializers (the outer length is the number of rows), and empty
initializers (`new int[] {}`, `int[] e = {};`).

## Before / after

```java
static void put(byte[] b, @refinement("_ >= 0 && _ <= length(b)") int n) { }
put(new byte[] {1, 2}, 2);
put(new byte[] {1, 2}, 3);
```

| call | main (e3b11c8) | this PR |
|---|---|---|
| `put(new byte[] {1, 2}, 2)` | `Refinement Error: found n == 2 but
expected n >= 0 && n <= length(b)`, counterexample `length(b) == 0` |
`Correct! Passed Verification.` |
| `put(new byte[] {1, 2}, 3)` | same error, counterexample `length(b) ==
0` | `Refinement Error: found n == 3 && length(b) == 2 but expected n >=
0 && n <= length(b)` |

I also checked these by hand: `int[]`, `String[]`, `int[][] {{1}, {2,
3}}` (length 2), `new int[] {}`, `int[] e = {}`, and
`@Refinement("length(_) == 2") int[] f = {4, 5}` all pass. The same
calls with a wrong length give a Refinement Error.

## Test

I extended `CorrectStringArrayLength` with one call that passes an
initializer and one that passes an empty declaration initializer, both
against an exact `_ == length(names)` refinement. The fixture fails on
main. I added no Error fixture, because main already reports an error
for a wrong length (only for the wrong reason), so such a fixture would
pass without the fix. The exact `==` refinement already catches a wrong
element count. The full `mvn test` passes locally.

## Relation to #400

#400 (`.length` on array expressions, spread varargs) gives a spread
varargs call a fresh array with `length == number of arguments`. This PR
handles explicit initializers, the case #400's `count(new String[] { "x"
})` passes through the normal argument path. The two PRs touch different
methods and do not conflict.

## Not changed here

`new int[2][3]` gets `length == 3`, because the loop over size
expressions keeps the last one (the existing `TODO only working for 1
dimension`). So `grid(new int[2][3], 3)` against `_ == length(b)` is
wrongly accepted. This bug is older than this PR and is not part of it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: "Sorts Int and Bool are incompatible" when a JDK method's result is passed to a parameter refined in an external spec

1 participant